package com.anjoyo.xgh.musicshare.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;

import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.MD5Util;

public class UserApi {

	Handler handler;

	public UserApi(Handler handler) {
		this.handler = handler;
	}

	public void regist(String name, String password, String nickname,
			String desc, String sex, File file) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		ajaxParams.put("name", name);
		ajaxParams.put("password", MD5Util.MD5(password));
		ajaxParams.put("sex", sex);
		try {
			ajaxParams.put("nickname", URLEncoder.encode(nickname, "UTF-8"));
			ajaxParams.put("desrption", URLEncoder.encode(desc, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		try {
			ajaxParams.put("file", file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		finalHttp.configCharset("GBK");
		finalHttp.post(Constant.REGIST_URL, ajaxParams,
				new AjaxCallBack<String>() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						System.out.println(t.toString());
						try {
							Message message = handler.obtainMessage();
							message.what = Constant.MSG_REGIST_OK;
							JSONObject jsonObject = new JSONObject(t.toString());
							if (jsonObject.getString("code").equals("200")) {
								message.arg1 = 200;
							} else {
								message.arg1 = 404;
							}
							message.obj = jsonObject;
							handler.sendMessage(message);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	public void oAuthRegist(HashMap<String, Object> res) {
		long id = Long.parseLong(res.get("id").toString());
		String desc = res.get("description").toString();
		String nickname = res.get("screen_name").toString();
		String sex = res.get("gender").toString();
		String iconString = res.get("avatar_large").toString();
		String smallicon = res.get("profile_image_url").toString();
		
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams ajaxParams = new AjaxParams();
		ajaxParams.put("name", id + "");
		System.out.println("用户名：" + id);
		ajaxParams.put("password", MD5Util.MD5(id + Constant.APP_KEY));
		ajaxParams.put("sex", sex);
		ajaxParams.put("nickname", nickname);
		ajaxParams.put("desrption", desc);
		ajaxParams.put("file", iconString);
		ajaxParams.put("small_icon", smallicon);
		// finalHttp.configCharset("GBK");
		// handler.sendEmptyMessage(Constant.MSG_GET_USERINFO_OK);
		finalHttp.get(Constant.REGIST_URL, ajaxParams,
				new AjaxCallBack<Object>() {

					@Override
					public void onSuccess(Object t) {
						super.onSuccess(t);
//						System.out.println(t.toString());
						try {
							Message message = new Message();
							message.what = Constant.MSG_REGIST_OK;
							JSONObject jsonObject = new JSONObject(t.toString());
							if (jsonObject.getString("code").equals("200")) {
								message.arg1 = Constant.MSG_SINA_SUCCESS;
								message.obj = t.toString();
							} else {
								message.arg1 = Constant.MSG_SINA_ERROR;
								message.obj = jsonObject.getString("msg");
							}
							handler.sendMessage(message);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	public void login(String name, String password) {
		FinalHttp finalHttp = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("name", name);
		params.put("password", MD5Util.MD5(password));
		finalHttp.post(Constant.LOGIN_URL, params, new AjaxCallBack<Object>() {

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				Message message = new Message();
				try {
					JSONObject object = new JSONObject(t.toString());
					if (object.getString("code").equals("200")) {
						message.what = Constant.MSG_LOGIN_SUCCESS;
						message.obj = t.toString();
					} else {
						message.what = Constant.MSG_LOGIN_ERROR;
						message.obj = object.getString("msg");
					}
					handler.sendMessage(message);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		});
	}
}
